<?php

namespace App\Rules;

use App\Repositories\Dictionary;
use Illuminate\Contracts\Validation\Rule;

class DictionaryRule implements Rule
{
    private $code;

    /**
     * Create a new rule instance.
     *
     * @param $code
     */
    public function __construct($code)
    {
        $this->code = $code;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        /** @var Dictionary $dictionary */
        $dictionary = app(Dictionary::class);
        return collect($dictionary->search($this->code))->pluck('name', 'code')->has($value);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The validation error message.';
    }
}
